package com.immortal.core.autoconfigure;

import com.immortal.core.module.quartz.AutowiredSpringBeanJobFactory;
import com.immortal.core.module.quartz.QuartzListenerAware;
import com.immortal.core.module.quartz.handler.DefaultQuartzTaskHandler;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

/**
 * Created by xukangkang on 2019/3/20.
 */
@ConditionalOnClass(value = Scheduler.class)
@Configuration
public class QuartzConfiguration {
    @Autowired
    private SchedulerFactoryBean schedulerFactoryBean;
    @Bean
    public AutowiredSpringBeanJobFactory autowiredSpringBeanJobFactory() {
        AutowiredSpringBeanJobFactory autowiredSpringBeanJobFactory = new AutowiredSpringBeanJobFactory();
        return autowiredSpringBeanJobFactory;
    }

    @Bean
    public Scheduler scheduler() {
        return schedulerFactoryBean.getObject();
    }

    @Bean
    public QuartzListenerAware quartzListenerAware() {
        QuartzListenerAware quartzListenerAware = new QuartzListenerAware();
        quartzListenerAware.setScheduler(scheduler());
        return quartzListenerAware;
    }

    @Bean
    public DefaultQuartzTaskHandler defaultQuartzTaskHandler() {
        DefaultQuartzTaskHandler defaultQuartzTaskHandler = new DefaultQuartzTaskHandler();
        defaultQuartzTaskHandler.setScheduler(scheduler());
        return defaultQuartzTaskHandler;
    }
}
